// pages/authorize/authorize.js
import {ajax} from "../../../utils/httpRequest";
import {URI_USERSTATUS, URI_QUANSHOU_POST_CODE, URI_POST_USERINFO} from "../../../utils/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_no_dang_yuan: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  bindGetUserInfo: function (e) {
    const that = this;
    const encryptedData = e.detail.encryptedData;
    const iv = e.detail.iv;
    const faceUrl = e.detail.userInfo.avatarUrl;//console.log(e.detail.userInfo.avatarUrl);
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      wx.login({
        success: (res) => {
          if (res.code) {
            const appid = 'wxea598e0f5823e621';
            const appsecret = '771a01e170a8edf8cc38fb7faac7aec9';
            ajax.get(URI_QUANSHOU_POST_CODE, {
              code: res.code,
              appid: appid,
              appsecret: appsecret,
              iv: iv,
              encryptedData: encryptedData
            }, {}, res => {
              wx.setStorageSync('only_u', res.uuuuuu);
              wx.setStorageSync('faceUrl', faceUrl);
              if (res.info) {
                wx.setStorageSync('uid', res.info.id);
                wx.setStorageSync('token', res.info.token);
                wx.setStorageSync('tel', res.info.tel);
                this.getUserStatus();
              } else {
                wx.reLaunch({
                  url: '/pages/entry/regist/regist',
                });
                return false;
              }
              //session_key res.data
              // ajax.post(URI_POST_USERINFO, {
              //   encryptedData: encryptedData,
              //   iv: iv,
              //   sessionkey: res.data,
              //   appid: appid,
              // }, {}, res => {
              //
              //   // 后端返回 userInfo
              //   // console.log(res.data.data);
              //   if (res.data.data.tel != '') {
              //     wx.setStorageSync('uid', res.data.data.uid);
              //     wx.setStorageSync('token', res.data.data.token);
              //     wx.setStorageSync('tel', res.data.data.tel);
              //     this.getUserStatus();
              //   }else {
              //
              //   }
              // })
            })
          }
        },
        fail() {
          wx.showToast({
            title: '异常，请重试',
            icon: 'none',
            duration: 3000
          })
        }
      })

    } else {
      //用户按了拒绝按钮
      wx.showToast({
        title: '只有授权才能登陆...',
        icon: 'none',
        duration: 2000
      })
    }
  },

  getUserStatus() {
    //* state=0 没有任何数据，从人脸录入开始
    // * state=1 只是缺少人脸数据
    // * state=2 所有信息都存在，但信息状态不能确定
    // * state=3 群众不能使用该功能
    ajax.get(URI_USERSTATUS, {
      uid: wx.getStorageSync('uid')
    }, {}, res => {
      wx.setStorageSync('dangyuan_id', res.data.dangyaun_id);
      if (res.data.state == 0) {
        wx.reLaunch({
          url: '/pages/submit_info/submit_info',
        })
      } else if (res.data.state == 1) {
        wx.reLaunch({
          url: '/pages/uploadpage/uploadpage'
        })
      } else if (res.data.state == 2) {
        wx.reLaunch({
          url: '/pages/user_manage/user_manage'
        })
      } else if (res.data.state == 3) {
        this.setData({
          show_no_dang_yuan: true
        })
        // wx.showModal({
        //   title:'温馨提示',
        //   content:'您不是党员不能使用该功能！',
        //   showCancel:false,
        //   showConfirm:false
        // })
      }
    })
  },
  onShareAppMessage() {
    return {
      title: '党员管理系统',
      path:'pages/entry/authorize/authorize',
      success:function () {
        console.log('转发成功');
      },
      file:function () {
        console.log('转发失败')
      }
    }
  }

})